﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class Orders : PageBase
    {
        string Key = string.Empty;
        string Status = string.Empty;

        BLL.Orders bll = new BLL.Orders();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("shopping", "read");

            if (!string.IsNullOrEmpty(Request.QueryString["key"]))
            {
                Key = Request.QueryString["key"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["status"]))
            {
                Status = Request.QueryString["status"];
            }

            BindData();

            if (!IsPostBack)
            {
                BindCategory();
                BindSearch();
            }
        }

        protected void BindCategory()
        {
            BLL.OrderStatus bcBll = new BLL.OrderStatus();
            DataSet ds = bcBll.GetAllList();

            ddlType.DataSource = ds.Tables[0].DefaultView;
            ddlType.DataValueField = "OrderStatusId";
            ddlType.DataTextField = "OrderStatusName";
            ddlType.DataBind();

            ddlType2.DataSource = ds.Tables[0].DefaultView;
            ddlType2.DataValueField = "OrderStatusId";
            ddlType2.DataTextField = "OrderStatusName";
            ddlType2.DataBind();

            ddlType.Items.Insert(0, new ListItem("请选择", ""));
            ddlType2.Items.Insert(0, new ListItem("请选择", ""));
        }

        protected void BindSearch()
        {
            txtKey.Value = Key;
            ddlType.SelectedValue = Status;
        }

        protected void BindData()
        {
            string strWhere = "IsActive=1";
            if (!string.IsNullOrEmpty(Key))
            {
                strWhere += " and name like '%" + Key + "%'";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                strWhere += " and status=" + Status;
            }
            //strWhere += " order by BookCommentId desc";
            int recordCount = bll.GetRecordCount(strWhere);
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = bll.GetListByPageV(strWhere, "OrderId desc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Orders.aspx?status=" + ddlType.SelectedValue + "&key=" + txtKey.Value.Trim());
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Check("shopping", "del");

            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                bll.DeleteByIds(ids);
            }

            Response.Redirect("Orders.aspx?status=" + ddlType.SelectedValue + "&key=" + txtKey.Value.Trim());
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids) && !string.IsNullOrWhiteSpace(ddlType2.SelectedValue))
            {
                bll.UpdateStatusByIds(ids, int.Parse(ddlType2.SelectedValue));
            }

            Response.Redirect("Orders.aspx?status=" + ddlType.SelectedValue + "&key=" + txtKey.Value.Trim());
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }
    }
}